﻿#include "stdafx.h"
#include "XTextEnvIntParserFilter.h"

#include "XBSTR.h"
#include "XString.h"
#include "StrUtil.h"
#include "XStrUtil.h"
#include "Util.h"
#include "IXTextEnv.h"

#include "ParseHelper.h"


CXTextEnvIntParserFilter::CXTextEnvIntParserFilter()
{
    ;
}

CXTextEnvIntParserFilter::~CXTextEnvIntParserFilter()
{
    ;
}

// 填充属性
BOOL CXTextEnvIntParserFilter::FillAttr(IN IXConfigNode* pNode, IN IXControl* pControl, IN IXControl* pParent)
{
    XBSTR bstrTemp;
    HRESULT hResult;
    BOOL bTemp;
    COLORREF clrTemp;
    DWORD dwTemp;

    IXTextEnv* pTextEnv = NULL;
    pControl->get_TextEnv(&pTextEnv);
    if(pTextEnv == NULL)
        Util::CreateObject(&pTextEnv);
    if(pTextEnv == NULL)
    {
        XASSERT_ERROR(pTextEnv != NULL, _T("get_TextEnv Failed"));
        return FALSE;
    }

    // TextColor
    hResult = pNode->GetValue(_T("textcolor"), &clrTemp);
    if(SUCCEEDED(hResult))
    {
        pTextEnv->put_TextColor(clrTemp);
    }

    // BackColor
    hResult = pNode->GetValue(_T("backcolor"), &clrTemp);
    if(SUCCEEDED(hResult))
    {
        pTextEnv->put_BackColor(clrTemp);
    }

    // Transparent
    hResult = pNode->GetValue(_T("texttrans"), &bTemp);
    if(SUCCEEDED(hResult))
    {
        pTextEnv->put_Transparent(bTemp);
    }

    // DrawFlags
    hResult = pNode->GetValue(_T("drawflags"), &bstrTemp);
    if(SUCCEEDED(hResult))
    {
        XString strText(bstrTemp);
        XStrVector vctResult;
        size_t nCount = XStrUtil::SplitString(strText, _T(";"), vctResult);
        dwTemp = 0;

        if(vctResult.size() > 0)
        {
            struct stData
            {
                LPCTSTR szName;
                DWORD   dwFlags;
            } Data[] =
            {
                _T("top"), DT_TOP,
                _T("left"), DT_LEFT,
                _T("center"), DT_CENTER,
                _T("right"), DT_RIGHT,
                _T("vcenter"), DT_VCENTER,
                _T("bottom"), DT_BOTTOM,
                _T("wordbreak"), DT_WORDBREAK,
                _T("singline"), DT_SINGLELINE,
                _T("expandtabs"), DT_EXPANDTABS,
                _T("tabstop"), DT_TABSTOP,
                _T("noclip"), DT_NOCLIP,
                _T("externalleading"), DT_EXTERNALLEADING,
                _T("noprefix"), DT_NOPREFIX,
                _T("internal"), DT_INTERNAL,

                _T("editcontrol"), DT_EDITCONTROL,
                _T("path"), DT_PATH_ELLIPSIS,
                _T("end"), DT_END_ELLIPSIS,
                _T("modifystring"), DT_MODIFYSTRING,
                _T("rtlreading"), DT_RTLREADING,
                _T("word"), DT_WORD_ELLIPSIS,
                _T("nofullwidthcharbreak"), DT_NOFULLWIDTHCHARBREAK,
                _T("hideprefix"), DT_HIDEPREFIX,
                _T("prefixonly"), DT_PREFIXONLY,
            };
            for(size_t i=0; i<nCount; ++ i)
            {
                for(int j=0; j<_countof(Data); ++ j)
                {
                    if(_tcsicmp(vctResult[i], Data[j].szName) == 0)
                    {
                        dwTemp |= Data[j].dwFlags;
                    }
                }
            }
            pTextEnv->put_DrawFlags(dwTemp);
        }
    }

    // SingleLine
    hResult = pNode->GetValue(_T("textsingle"), &bTemp);
    if(SUCCEEDED(hResult))
    {
        pTextEnv->put_Singleline(bTemp);
    }

    pControl->put_TextEnv(pTextEnv);
    if(pControl->IsAutoSize())
        pControl->AutoSize();

    return TRUE;
}
